package com.umakr.ax.security.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.umakr.ax.security.model.Res;
import com.umakr.ax.security.model.Role;
import com.umakr.ax.security.model.RoleRes;
import com.umakr.ax.security.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author gx
 * @since 2017/5/15
 */
@Controller
@RequestMapping("/api/system/roleres")
public class PermissionRoleResController {
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    ResService resService;
    @Autowired
    RoleResService roleResService;
    /**
     * (H-F)22
     * @return 结果
     */
    @ApiOperation("设置角色资源")
    @RequestMapping("/configroleresource")
    @ResponseBody
    public Object configroleresource(Long roleId,Long[] resourceId){
        Role role =  roleService.selectById(roleId);
        if(role == null){
            return roleService.resultMap(0,"role is not exit.");
        }
        for(Long resId : resourceId){
            Res res = resService.selectById(resId);
            if(res != null){
                EntityWrapper<RoleRes> entityWrapper = new EntityWrapper<>();
                entityWrapper.eq("res_id",resId);
                entityWrapper.eq("role_id",roleId);
                List<RoleRes> roleResList = roleResService.selectList(entityWrapper);
                if(roleResList.isEmpty()){
                    RoleRes roleRes = new RoleRes();
                    roleRes.setResId(resId);
                    roleRes.setRoleId(roleId);
                    roleResService.update(roleRes);
                }
            }
        }
        return resService.resultMap(1,"");
    }

    /**
     * (H-F)23
     * @return 结果
     */
    @ApiOperation("取消角色资源")
    @RequestMapping("/cancelroleresource")
    public Object cancelroleresource(Long roleId,Long[] resourceId){
        Role role =  roleService.selectById(roleId);
        if(role == null){
            return roleService.resultMap(0,"role is not exit.");
        }
        for(Long resId : resourceId){
            Res res = resService.selectById(resId);
            if(res != null){
                EntityWrapper<RoleRes> entityWrapper = new EntityWrapper<>();
                entityWrapper.eq("res_id",resId);
                entityWrapper.eq("role_id",roleId);
                List<RoleRes> roleResList = roleResService.selectList(entityWrapper);
                if(!roleResList.isEmpty()){
                   for(RoleRes roleRes : roleResList){
                       roleResService.delete(roleRes.getId());
                   }
                }
            }
        }
        Map<String,Object> map = resService.resultMap(1,"");
        return ResponseEntity.ok(map);
    }

}
